{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# WebSurferAgent\n",
    "\n",
    "In [`browser-use tool`](https://github.com/ag2ai/ag2/blob/main/notebook/tools_browser_use.ipynb) and [`crawl4ai tool`](https://github.com/ag2ai/ag2/blob/main/notebook/tools_crawl4ai.ipynb) notebooks, we demonstrated how to create Agents with basic web surfing capabilities.\n",
    "\n",
    "Now, we're taking it a step further with `WebSurferAgent`—a powerful agent equipped with built-in web surfing tools right out of the box!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WebSurferAgent with `browser-use` tool\n",
    "\n",
    "**Warning:**\n",
    "[`Browser Use`](https://github.com/browser-use/browser-use) requires **Python 3.11 or higher**.\n",
    "\n",
    "### Installation\n",
    "\n",
    "To get started with the `browser-use` integration in AG2, follow these steps:\n",
    "\n",
    "1. Install AG2 with the `browser-use` extra:\n",
    "   ```bash\n",
    "   pip install -U ag2[openai,browser-use]\n",
    "   ```\n",
    "   > **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "   > ```bash\n",
    "   > pip install -U autogen[openai,browser-use]\n",
    "   > ```\n",
    "   > or  \n",
    "   > ```bash\n",
    "   > pip install -U ag2[openai,browser-use]\n",
    "   > ```\n",
    "   > as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "2. Set up Playwright:\n",
    "   \n",
    "   ```bash\n",
    "   # Installs Playwright and browsers for all OS\n",
    "   playwright install\n",
    "   # Additional command, mandatory for Linux only\n",
    "   playwright install-deps\n",
    "   ```\n",
    "\n",
    "3. For running the code in Jupyter, use `nest_asyncio` to allow nested event loops.\n",
    "    ```bash\n",
    "    pip install nest_asyncio\n",
    "    ```\n",
    "\n",
    "\n",
    "You're all set! Now you can start using browsing features in AG2.\n",
    "\n",
    "\n",
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import nest_asyncio\n",
    "\n",
    "from autogen.agentchat import UserProxyAgent\n",
    "from autogen.agents.experimental import WebSurferAgent\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `browser-use` WebSurferAgent\n",
    "\n",
    "> **Note:** [`Browser Use`](https://github.com/browser-use/browser-use) supports the following models: [Supported Models](https://docs.browser-use.com/customize/supported-models#supported-models)\n",
    ">\n",
    "> We had great experience with `OpenAI`, `Anthropic`, and `Gemini`. However, `DeepSeek` and `Ollama` haven't performed as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list = [{\"model\": \"gpt-5-nano\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n",
    "\n",
    "llm_config = {\n",
    "    \"config_list\": config_list,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two ways to start a chat session which is using only one agent with LLM configuration.\n",
    "\n",
    "#### **Recommended:** Using the `run` Method\n",
    "\n",
    "The new `run` method simplifies the process by eliminating the need for manual `UserProxyAgent` creation.\n",
    "\n",
    "- ✅ **Easier setup** – No need to manually register tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The `web_tool=\"browser_use\"` tells the agent to use the `BrowserUseTool` to surf the web.\n",
    "websurfer = WebSurferAgent(name=\"WebSurfer\", llm_config=llm_config, web_tool=\"browser_use\")\n",
    "\n",
    "run_response = websurfer.run(\n",
    "    message=\"Get info from https://docs.ag2.ai/docs/Home\",\n",
    "    tools=websurfer.tools,\n",
    "    max_turns=2,\n",
    "    user_input=False,\n",
    ")\n",
    "run_response.process()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Manual Setup:** Using `initiate_chat` Method\n",
    "This method requires manually creating a `UserProxyAgent` and registering tools for execution.\n",
    "\n",
    "- ⚠️ **More setup required**\n",
    "- ⚠️ **Must manually register tools**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "websurfer = WebSurferAgent(name=\"WebSurfer\", llm_config=llm_config, web_tool=\"browser_use\")\n",
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\")\n",
    "# WebSurferAgent has a list of tools which are registered for LLM\n",
    "# We need to register the tools for execution with the UserProxyAgent\n",
    "for tool in websurfer.tools:\n",
    "    tool.register_for_execution(user_proxy)\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    recipient=websurfer,\n",
    "    message=\"Get info from https://docs.ag2.ai/docs/Home\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WebSurferAgent with `crawl4ai` tool\n",
    "\n",
    "### Installation\n",
    "\n",
    "To get started with the `crawl4ai` integration in AG2, follow these steps:\n",
    "\n",
    "1. Install AG2 with the `crawl4ai` extra:\n",
    "   ```bash\n",
    "   pip install -U ag2[openai,crawl4ai]\n",
    "   ```\n",
    "   > **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "   > ```bash\n",
    "   > pip install -U autogen[openai,crawl4ai]\n",
    "   > ```\n",
    "   > or  \n",
    "   > ```bash\n",
    "   > pip install -U ag2[openai,crawl4ai]\n",
    "   > ```\n",
    "   > as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "2. Set up Playwright:\n",
    "   \n",
    "   ```bash\n",
    "   # Installs Playwright and browsers for all OS\n",
    "   playwright install\n",
    "   # Additional command, mandatory for Linux only\n",
    "   playwright install-deps\n",
    "   ```\n",
    "\n",
    "3. For running the code in Jupyter, use `nest_asyncio` to allow nested event loops.\n",
    "    ```bash\n",
    "    pip install nest_asyncio\n",
    "    ```\n",
    "\n",
    "\n",
    "You're all set! Now you can start using browsing features in AG2.\n",
    "\n",
    "\n",
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import nest_asyncio\n",
    "\n",
    "from autogen.agentchat import UserProxyAgent\n",
    "from autogen.agents.experimental import WebSurferAgent\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Crawl4AI WebSurferAgent\n",
    "\n",
    "> **Note:** [`Crawl4AI`](https://github.com/unclecode/crawl4ai) is built on top of [LiteLLM](https://github.com/BerriAI/litellm) and supports the same models as LiteLLM.\n",
    ">\n",
    "> We had great experience with `OpenAI`, `Anthropic`, `Gemini` and `Ollama`. However, as of this writing, `DeepSeek` is encountering some issues.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list = [{\"model\": \"gpt-5-nano\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}]\n",
    "\n",
    "llm_config = {\n",
    "    \"config_list\": config_list,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two ways to start a chat session which is using only one agent with LLM configuration.\n",
    "\n",
    "#### **Recommended:** Using the `run` Method\n",
    "\n",
    "The new `run` method simplifies the process by eliminating the need for manual `UserProxyAgent` creation.\n",
    "\n",
    "- ✅ **Easier setup** – No need to manually register tools\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# `web_tool` parameter must be set to `crawl4ai` in order for the `Crawl4AITool` to be used.\n",
    "websurfer = WebSurferAgent(name=\"WebSurfer\", llm_config=llm_config, web_tool=\"crawl4ai\")\n",
    "\n",
    "run_response = websurfer.run(\n",
    "    message=\"Get info from https://docs.ag2.ai/docs/Home\",\n",
    "    tools=websurfer.tools,\n",
    "    max_turns=2,\n",
    "    user_input=False,\n",
    ")\n",
    "run_response.process()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Manual Setup:** Using `initiate_chat` Method\n",
    "This method requires manually creating a `UserProxyAgent` and registering tools for execution.\n",
    "\n",
    "- ⚠️ **More setup required**\n",
    "- ⚠️ **Must manually register tools**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\")\n",
    "websurfer = WebSurferAgent(name=\"WebSurfer\", llm_config=llm_config, web_tool=\"crawl4ai\")\n",
    "\n",
    "# WebSurferAgent has a list of tools which are registered for LLM\n",
    "# We need to register the tools for execution with the UserProxyAgent\n",
    "for tool in websurfer.tools:\n",
    "    tool.register_for_execution(user_proxy)\n",
    "\n",
    "user_proxy.initiate_chat(\n",
    "    recipient=websurfer,\n",
    "    message=\"Get info from https://docs.ag2.ai/docs/Home\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "WebSurfer Agent",
   "tags": [
    "agents",
    "browser-use",
    "crawl4ai",
    "webscraping",
    "function calling"
   ]
  },
  "kernelspec": {
   "display_name": ".venv-crawl4ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
